<?php
/**
  * Musashi - A project manager for music groups and bands
  * Copyright (C) 2010 Daniel Torres
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */

/**
 * 
 *  
 * @package Musashi
 * @author Daniel Torres
 */
 
 // Make sure we have access to this page
 defined('_JEXEC') or die ('Restricted access. Sorry');
 
 jimport('joomla.application.component.view');
 
 /**
 * 
 * Prepares and displays the catalog entries for the requested view
 *
 * @package Musashi 
 * @author Daniel Torres
 */
 class MusashiAdminViewCatalog extends JView
 {
    /**
     *  Prepares the information needed to be displayed
     */
    function display( $tpl = null )
    {
      // We will be needing the model to access some data:
      $model =& $this->getModel();
                  
      // Find out if we are editing (a layout has been requested) or showing
      // (a catalog name has been requested)
      $layout = JRequest::getVar('layout','none');
            
      // If no layout has been specified, we are simply trying to show some records
      if( $layout == 'none')
      {
         
         // Tell the model to process the requested query
         $model->readCatalogEntries();
         $viewName = $model->getSectionName();
         $viewTitle = $model->getSectionTitle();
         $order =& $model->getDataSortOrder();
   
         JToolBarHelper::title( JText::_($viewTitle), 'generic.png');
         JToolBarHelper::deleteList();
         JToolBarHelper::editListX();
         JToolBarHelper::addNewX();
               
         // Read data and display info:      
         $dataRows =& $model->getSectionData();
         $this->assignRef('dataRows',$dataRows);
         $this->assignRef('viewName',$viewName);
         $this->assignRef('viewTitle',$viewTitle);
         $this->assignRef('lists',$order);
                  
         // If we actually have something to display, display it here      
         if( $dataRows != null )
         {
            // Display a template of the requested name
            parent::display( $viewName );
         }
         else
         {
            // We have nothing to display
            parent::display("empty");        
         }		
      }
      // Otherwise, we have been requested to either add or edit a record. Get
      // the record itself, and show it:
      else
      {
         // Make sure we are not displaying a form (they start with modal_)
         if( strpos($layout,"modal_") === false )
         {
            // Get data from the model
            $model->readCatalogRecord();
            $record =& $model->getRecordData();
            $viewName = $model->getSectionName();
            $viewTitle = $model->getSectionTitle();
            
            // The new field is only set if we are adding a record
            $isNew = ($record->id < 1);        
            $text  = $isNew? JText::_('New') : JText::_('Edit');
                   
            // Set the toolbar
            JToolBarHelper::title( JText::_($viewTitle).': <small><small>['.$text.']</small></small>' );
            JToolBarHelper::save();
           
            if( $isNew ){
                  JToolBarHelper::cancel();
            } else {
                  JToolBarHelper::cancel('cancel','Close');
            }
            
            $this->assignRef('viewName', $viewName);
            $this->assignRef('record', $record );
            $this->assignRef('viewTitle',$viewTitle);         
            
         }
         
         parent::display( $tpl );

      }
    }    
 }
?>
